import { hasToken } from '@/utils/storage'
import { Route, Redirect, RouteProps } from 'react-router-dom'

export const PrivateRoute = ({ children, ...rest }: RouteProps) => {
  console.log(rest)
  //   const { children, ...rest } = obj
  return (
    <Route
      {...rest}
      render={props => {
        // 如果有token就渲染 children
        if (hasToken()) {
          return children
        }
        return (
          <Redirect
            to={{
              pathname: '/login',
              state: {
                from: props.location.pathname // 回跳地址
              }
            }}
          />
        )
      }}
    />
  )
}
